package com.baizhi.controller;

import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lenovo on 2019/6/17.
 */
@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    //登录
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){
        Admin admin = adminService.selectOne(username, password);
        if (admin!=null){
            session.setAttribute("admin",admin);
            return "redirect:main/main.jsp";
        }else {
            return "login";
        }
    }

    //修改密码
    @RequestMapping("/update")
    public @ResponseBody Map update(Admin admin){
        Map map=new HashMap();
        try {
            adminService.update(admin);
            map.put("updateok",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("updateok",false);
        }
        return  map;
    }
    //退出登录
    @RequestMapping("/destory")
    public String destroy(HttpSession session){
        session.getAttribute("admin");
        session.removeAttribute("admin");
        return "login";
    }
}
